using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;

namespace SimplePoker.Main
{
    public class Tournament
    {
        private PlayerBase _Player1;
        private PlayerBase _Player2;
        private int _Rounds;

        public bool HasEnded { get { return (_Player1.ChipCount < 1 || _Player2.ChipCount < 1); } }

        public Tournament(PlayerBase p1, PlayerBase p2)
        {
            if (p1 == p2)
                throw new ArgumentException("cannot start tournament with a player playing agaist themselves");

            if (p1 == null || p2 == null)
                throw new ArgumentException("players cannot be null.");
            _Player1 = p1;
            _Player2 = p2;
            _Rounds = 0;

        }

        public PlayerBase GetWinner()
        {
            Complete();

            if (_Player2.ChipCount == 0)
                return _Player1;
            else
                return _Player2;
        }

        private void Complete()
        {
            while (!HasEnded)
            {
                Round round;
                _Rounds++;

                if (_Rounds % 2 == 0)
                    round = new Round(_Player1, _Player2);
                else
                    round = new Round(_Player2, _Player1);

                _Player1.NotifyNewHand();
                _Player2.NotifyNewHand();

                while (!round.HasEnded)
                {
                    round.DoNextAction();
                }

            }
        }
    }
}
